<?php
//This Code is published under the conditions of the CDDL (Sun Microsystems) (c) 2008 by Fabian Tschirschnitz
	class SequenceDrawer{
	//gibt einen DNA-String als Grafik aus
		function draw_dna_sequence($seq){
			$i=0;
			echo "<strong><h1>3' >></h1></strong><p class=\"rightTxt\">";
			foreach(str_split($seq,1) as $bases){
				
				if($i%15==0 && $i!= 0){echo "{$i}<br/>";}
				$i++;		
				switch($bases){
					case A: echo "<img src=\"images/adenin.png\" alt=\"{$i}\"/>"; break;
					case T: echo "<img src=\"images/thymin.png\" alt=\"{$i}\"/>"; break;
					case G: echo "<img src=\"images/guanin.png\" alt=\"{$i}\"/>"; break;
					case C: echo "<img src=\"images/cytosin.png\" alt=\"{$i}\"/>"; break;
					case a: echo "<img src=\"images/adeninmatch.png\" alt=\"{$i}\"/>"; break;
					case t: echo "<img src=\"images/utmatch.png\" alt=\"{$i}\"/>"; break;
					case g: echo "<img src=\"images/guaninmatch.png\" alt=\"{$i}\"/>"; break;
					case c: echo "<img src=\"images/cytosinmatch.png\" alt=\"{$i}\"/>"; break;
					case W: echo "<img src=\"images/wildcardmatch.png\" alt=\"{$i}\"/>"; break;
					default: echo "<img src=\"images/na.png\" alt=\"{$i}\"/>";
					
				}			
			}echo $i;
			echo "<strong><h1>>> 5'</h1></strong></p>";
					
		}
		
	   function print_dna_sequence($seq)	{
		   $seq = chunk_split($seq, 50, "<br/>"); 
		   echo $seq;
	  }
	  //gibt einen RNA-String als Grafik aus
	  function draw_rna_sequence($seq){
			$i=0;
			echo "<strong><h1>5' >></h1></strong><p class=\"rightTxt\">";
			foreach(str_split($seq,1) as $bases){
				
				if($i%15==0 && $i!= 0){echo "{$i}<br/>";}
				$i++;		
				switch($bases){
					case A: echo "<img src=\"images/adenin.png\" alt=\"{$i}\"/>"; break;
					case U: echo "<img src=\"images/uracil.png\" alt=\"{$i}\"/>"; break;
					case G: echo "<img src=\"images/guanin.png\" alt=\"{$i}\"/>"; break;
					case C: echo "<img src=\"images/cytosin.png\"alt=\"{$i}\"/>"; break;
					case a: echo "<img src=\"images/adeninmatch.png\" alt=\"{$i}\"/>"; break;
					case u: echo "<img src=\"images/utmatch.png\" alt=\"{$i}\"/>"; break;
					case g: echo "<img src=\"images/guaninmatch.png\" alt=\"{$i}\"/>"; break;
					case c: echo "<img src=\"images/cytosinmatch.png\" alt=\"{$i}\"/>"; break;
					case W: echo "<img src=\"images/wildcardmatch.png\" alt=\"{$i}\"/>"; break;
					default: echo "<img src=\"images/na.png\"alt=\"{$i}\"/>";
				}			
			}echo $i;
			echo "<strong><h1>>> 3'</h1></strong></p>";
					
		}
		
		function print_rna_sequence($seq)	{
		   $seq = chunk_split($seq, 50, "<br/>");  
		   echo $seq;
	  }
	  
	  function print_as_sequence($seq)	{
	      $seq = chunk_split($seq,1, " ");
		   $seq = chunk_split($seq, 50, "<br/>");  
		   echo $seq;
	  }
	  //erstellt aus einem Statistikarray heraus eine SVG-Chart und speichert es ab!
	  function draw_dna_stats($stats){
	   
	  //veränderte Variablen für das Chart
	  $cpa=$stats[pa]*2;
	  $cpt=$stats[pt]*2;
	  $cpg=$stats[pg]*2;
	  $cpc=$stats[pc]*2;
		
	  $r=rand(1,1024);
	  $filedirectory = "../";
	  $filename = "images/charts/chart{$r}.svg";
	  $filecontent=
	   "<?xml version='1.0' encoding='utf-8'?>

	   <svg xmlns='http://www.w3.org/2000/svg'
	   xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:ev='http://www.w3.org/2001/xml-events'
	   version='1.1' baseProfile='full'
	   width='300' height='120'>
	
	   <!--Inhalt der Datei -->
	   <text x='0' y='10' font-size='10px' fill='#555555'>|0%</text>
	   <text x='50' y='10' font-size='10px' fill='#555555'>|25%</text>
  	   <text x='100' y='10' font-size='10px' fill='#555555'>|50%</text>
		<text x='200' y='10' font-size='10px' fill='#555555'>|100%</text>
		<text x='250' y='20' font-size='10px' fill='#555555'>Adenin</text>
		<text x='250' y='42' font-size='10px' fill='#555555'>Thymin</text>
		<text x='250' y='64' font-size='10px' fill='#555555'>Guanin</text>
		<text x='250' y='86' font-size='10px' fill='#555555'>Cytosin</text>
		<rect x='0' y='15' width='{$cpa}' height='15' fill='#db211d' stroke='black'/>
		<rect x='0' y='35' width='{$cpt}' height='15' fill='#98ca2e' stroke='black'/>
		<rect x='0' y='55' width='{$cpg}' height='15' fill='#287795' stroke='black'/>
		<rect x='0' y='75' width='{$cpc}' height='15' fill='#ffb617' stroke='black'/>
		</svg>"
		;
	
		$fhandle = fopen($filedirectory.$filename, "w+");
		fwrite($fhandle, $filecontent);
		fclose($fhandle);
		
		echo "<object data='{$filename}' type='image/svg+xml' width='300' height='120'><a href='{$filename}'><strong>Grafik</strong></a></object>";     
	  }
	  
	  function print_dna_stats($stats)	{
	   	echo "Adenin-Gehalt: <strong>".$stats[a]."   ~".round($stats[pa])."%</strong><br/>";
			echo "Thymin-Gehalt: <strong>".$stats[t]."   ~".round($stats[pt])."%</strong><br/>";
			echo "Guanin-Gehalt: <strong>".$stats[g]."   ~".round($stats[pg])."%</strong><br/>";
			echo "Cytosin-Gehalt: <strong>".$stats[c]."   ~".round($stats[pc])."%</strong><br/>";
	   	echo "unbekannte Zeichen: <strong>".$stats[na]."   ~".round($stats[pna])."%</strong><br/>";
	   	echo "Der GC-Gehalt liegt bei <strong>"."~".round($stats[pg_c])."%</strong>.<br/><br/>";
	   	echo "Die Sequenz hat eine Laenge von <strong>".$stats[length]." Basen</strong>!";
	  }
	}

?>